package com.lovo.project.servlet.service;

import com.lovo.project.enums.ServiceCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ServiceDO;
import com.lovo.project.service.ServiceService;
import com.lovo.project.service.impl.ServiceServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdateServiceServlet" ,value = "/service/updateService")
public class UpdateServiceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Integer serviceId = Integer.parseInt(request.getParameter("serviceId"));
            String serviceName = request.getParameter("serviceName");
            String serviceClass = request.getParameter("serviceClass");
            String serviceCuttingSpecification = request.getParameter("serviceCuttingSpecification");
            String serviceTimeConsuming = request.getParameter("serviceTimeConsuming");
            String serviceEmployGrade = request.getParameter("serviceEmployGrade");
            Integer serviceMoney = Integer.parseInt(request.getParameter("serviceMoney"));
            String serviceSchedule = request.getParameter("serviceSchedule");
            String serviceSpecification = request.getParameter("serviceSpecification");
            ServiceDO serviceDO =new ServiceDO();
            serviceDO.setServiceId(serviceId);
            serviceDO.setServiceName(serviceName);
            serviceDO.setServiceClass(serviceClass);
            serviceDO.setServiceCuttingSpecification(serviceCuttingSpecification);
            serviceDO.setServiceSpecification(serviceSpecification);
            serviceDO.setServiceTimeConsuming(serviceTimeConsuming);
            serviceDO.setServiceEmployGrade(serviceEmployGrade);
            serviceDO.setServiceMoney(serviceMoney);
            serviceDO.setServiceSchedule(serviceSchedule);
            ServiceService serviceService = new ServiceServiceImpl();
            Integer integer = serviceService.updateService(serviceDO);
            response.getWriter().print(ResultVO.success().add(integer).toJson());


        }catch (OurException e){
            response.getWriter().print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            response.getWriter().print(ResultVO.fail(ServiceCodeEnum.SERVICE_UNKNOWN_ERROR).toJson());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
